package imsdk

import (
	"encoding/json"
	"longmen/server/config/global"
	"longmen/server/pkg/common/helper/http"
)

type (
	CreateAccountRequest struct {
		UserID  string `json:"UserID"`  // 用户ID
		Nick    string `json:"Nick"`    // 昵称
		FaceURL string `json:"FaceUrl"` // 头像
	}
	CreateAccountResponse struct {
		ActionStatus string `json:"ActionStatus"`
		ErrorInfo    string `json:"ErrorInfo"`
		ErrorCode    int    `json:"ErrorCode"`
	}
)

// CreateAccount 创建账号
func (s sdk) CreateAccount(req CreateAccountRequest) bool {
	reqUrl := s.BuildRestfulUrl("/im_open_login_svc/account_import")
	bytes, err := json.Marshal(req)
	if err != nil {
		global.Log.Error("create account error: ", err)
		return false
	}
	result, err := http.HttpPost(reqUrl, string(bytes), connTimeoutMs, serveTimeoutMs, "application/json")
	if err != nil {
		global.Log.Errorf("err: %+v", err)
		return false
	}
	resp := CreateAccountResponse{}
	err = json.Unmarshal(result, &resp)
	if err != nil {
		global.Log.Error("create account Unmarshal error: ", err)
		return false
	}
	if resp.ActionStatus == FailActionStatus {
		global.Log.Error("create account  error: ", err, "error message : ", resp.ErrorInfo, "error code:", resp.ErrorCode)
		return false
	}
	return true
}
